﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Core.ProcessLoaderFiction;
using Core.ProcessManager.Process;

namespace View.ProcessSerializer
{
    public class ProcessPacker
    {
        static void Main()
        {
            var processLoader = new ProcessLoader();
            var processes = processLoader.LoadAll();
            var procType = processes.FirstOrDefault().GetType();
            var actType = processes.FirstOrDefault().Actions.FirstOrDefault().GetType();

            Serialize(processes, procType, actType);
        }

        private static void Serialize(IEnumerable<IProcess> processes, Type procType, Type actType)
        {
            var procSerializer = new XmlSerializer(procType);
            var actSerializer = new XmlSerializer(actType);

            foreach (var process in processes)
            {
                var streamWriter = new StreamWriter(string.Format("{0}.xml", process.Name));
                procSerializer.Serialize(streamWriter, process);

                foreach (var action in process.Actions)
                {
                    actSerializer.Serialize(streamWriter, action);
                }
                streamWriter.Close();
            }

            Console.Write("Done... Press any key to exit");
            Console.ReadKey();
        }
    }
}
